import _ from "lodash";
import DataStore from "../store/index";

export default class BaseController {
  $scope: DataStore;
  $api: string;

  constructor(store: { scope: DataStore }) {
    this.$scope = store.scope;
    this.$api = _.get(this, "__proto__.$api") as string;
  }
}

export const mixins = (derivedCtor: any, baseCtors: any[]) => {
  baseCtors.forEach((baseCtor) => {
    Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
      if (typeof derivedCtor.prototype[name] === "undefined") {
        derivedCtor.prototype[name] = baseCtor.prototype[name];
      } else {
        if (typeof baseCtor.prototype[name] === "object") {
          if (baseCtor.prototype[name] instanceof Array) {
            derivedCtor.prototype[name] = baseCtor.prototype[name].concat(
              derivedCtor.prototype[name]
            );
          } else {
            derivedCtor.prototype[name] = Object.assign(
              baseCtor.prototype[name],
              derivedCtor.prototype[name]
            );
          }
        }
      }
    });
  });
};
